﻿Public Interface IActivator
    Function createInstance() As Object
End Interface

<GlobalAvalibleObjectImplementation(singleton:=True)>
Public Class FastActivator

    Private _types As New Dictionary(Of Type, IActivator)

    Private Class FastActivator(Of T As New)
        Implements IActivator

        Public Function createInstance() As Object Implements IActivator.createInstance
            Return New T
        End Function
    End Class

    Private generic As Type = GetType(FastActivator(Of ))

    Public Function createInstance(type) As Object
        If _types.ContainsKey(type) Then Return _types(type).createInstance()
        Dim this_type As Type = generic.MakeGenericType(type)
        Dim fast_activator As IActivator = Activator.CreateInstance(this_type)
        _types(type) = fast_activator
        Return fast_activator.createInstance

    End Function

    Public Function getActivator(type) As IActivator
        If _types.ContainsKey(type) Then Return _types(type)
        Dim this_type As Type = generic.MakeGenericType(type)
        Dim fast_activator As IActivator = Activator.CreateInstance(this_type)
        _types(type) = fast_activator
        Return fast_activator
    End Function

End Class


